package left_right_pointers;

public class TwoSum {

    public int[] twoSum(int[] nums, int target) {
        // 一左一右两个指针相向而行
        int l = 0, r = nums.length - 1;
        int sum = 0;
        while (l <= r) {
            sum = nums[l] + nums[r];
            // 题目要求的索引从1开始
            if (sum == target) return new int[]{l + 1, r + 1};
            else if (sum < target) l++;
            else r--;
        }
        return new int[]{-1, -1};
    }
}
